<!--
 * @Description: 
 * @LastEditors: xzh
 * @LastEditTime: 2024-07-01 23:36:36
-->
<template>
  <DileTable ref="dileTableRef">
    <template #preColumn>
      <vxe-column
        field="equipmentCode"
        :title="
          $t('page.dashboardManageArr.afterSaleDashboardArr.equipStateArr.code')
        "
      />
      <vxe-column
        field="abnormal"
        :title="
          $t(
            'page.dashboardManageArr.afterSaleDashboardArr.equipStateArr.abnormal'
          )
        "
      />
      <vxe-column
        field="warningTime"
        :title="
          $t(
            'page.dashboardManageArr.afterSaleDashboardArr.equipStateArr.warningTime'
          )
        "
        width="90"
      />
    </template>
  </DileTable>
  <!-- 维修记录表单 -->
  <RepairRecordFrom ref="repairRecordFromRef" @refresh="handleRecordSave" />
</template>

<script setup lang="ts">
import { provide, reactive, ref, toRefs } from "vue";
import type { propsParam_Inf } from "@/components/dile-table/index.type";
import { object_Inf } from "@/utils/typings/index.type";
import { dayjs } from "element-plus";
import DileTable from "@/components/dile-table/index.vue";
import RepairRecordFrom from "@/views/equipManage/equipRepair/repairRecord/repairRecord-form/fom-drawer/index.vue";

const emit = defineEmits(["selectAbnormal"]);

/**
 * @Author: xzh
 * @Description: 表格实例
 * @Param:
 */
let dileTableRef = ref<any>(null);
/**
 * @Author: xzh
 * @Descripttion: 维修记录表单实例
 * @Param:
 */
let repairRecordFromRef = ref<any>(null);
/**
 * @Author: xzh
 * @Description: 加载表格数据
 * @Param:
 * @param {*} dataArr
 */
let init = (dataArr: Array<object_Inf>) => {
  let { tableData }: object_Inf = toRefs(propsParam);
  let row = dataArr.at(0) || {};
  //默认选中一行
  handleRowClick(row);
  tableData.value = dataArr;
};
/**
 * @Author: xzh
 * @Description: 行点击事件
 * @Param:
 * @param {*} row
 */
let handleRowClick = (row: object_Inf) => {
  let { setRadioRow = () => {} } = dileTableRef.value.getVxeTableRef();
  setRadioRow(row);
  emit("selectAbnormal", row);
};
/**
 * @Author: xzh
 * @Description: 行双击事件
 * @Param:
 * @param {*} row
 */
let handleDblClick = async (row: object_Inf) => {
  let {
    id,
    abnormal,
    warningTime,
    repairRecordId,
    equipmentId,
    equipmentCode,
    equipmentName,
    equipmentSpecification,
    equipmentFactoryName,
    equipmentTypeName,
    equipmentPositionName,
  } = row || {};
  //加载维修记录信息
  repairRecordFromRef.value.init(repairRecordId, {
    faultId: id,
    faultDate: dayjs(warningTime).format("YYYY-MM-DD HH:mm:ss"),
    faultDesc: abnormal,
    //拼接设备信息
    equipmentId,
    relEquip: {
      name: equipmentName, //设备名称
      code: equipmentCode, //设备编码
      specification: equipmentSpecification, //规格型号
      extJson: {
        factoryName: equipmentFactoryName, //所在工厂名称
        typeName: equipmentTypeName, //设备类型名称
        positionName: equipmentPositionName, //存放位置
      },
    },
  });
};
/**
 * @Author: xzh
 * @Description: 异常信息保存
 * @Param:
 * @param {*} recordInfo
 */
let handleRecordSave = (recordInfo: object_Inf) => {
  let { tableData } = toRefs(propsParam);
  let { id: recordId, faultId } = recordInfo;
  //根据异常信息id获取行
  let row = tableData?.value?.find((x) => x.id == faultId) || {};
  Object.keys(row).length > 0 &&
    Object.assign(row, {
      repairRecordId: recordId,
    });
};
/**
 * @Author: xzh
 * @Descripttion: 表格参数
 * @Param:
 */
let propsParam = reactive<propsParam_Inf>({
  loading: false,
  notNeedNumber: true,
  radio: true,
  tableData: [],
  isPagination: false,
  emitEvents: {
    cellClick: handleRowClick,
    cellDblClick: handleDblClick,
  },
});
provide("propsParam", propsParam);

defineExpose({
  init,
});
</script>
<style lang="scss" scoped>
.dile-table-container {
  width: 100%;
}
</style>
